<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../css/bootstrap.min.css">
</head>


<body>
    <!-- 给一个容器 -->
    <div class="container">
        <div class="row text-center">
            <h1>学生信息管理系统</h1>
            <hr>

            <a href="/index.html" class="btn btn-success">首页</a>
            <a href="/add.html" class="btn btn-warning">添加</a>

            <br>
            <br>
        </div>


        <div class="row">

            <table id="tb" class="table table-bordered table-condensed table-hover table-responsive text-center">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>年龄</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody></tbody>
                <!-- 将result的数据遍历出来 -->
            </table>

        </div>
    </div>

</body>

</html>
<script src="/js/jquery.min.js"></script>
<script>
    // let tb = document.querySelector("#tb");
    // let xhr = new XMLHttpRequest();
    // xhr.open("get","/index",true);
    // xhr.send();
    // xhr.onreadystatechange=function(e){
    //     // console.log(JSON.parse(xhr.response).data);
    //     if(xhr.readyState!==4){
    //         return
    //     }
    //     let newData = xhr.response?JSON.parse(xhr.response).data:[];
    //     console.log(newData);
    //     newData.forEach((item)=>{
    //         tb.innerHTML +=`
    //                 <tr class="data">
    //                     <td>${item.id}</td>
    //                     <td>${item.name}</td>
    //                     <td>${item.sex}</td>
    //                     <td>${item.age}</td>
    //                     <td>
    //                         <a href="" class="btn btn-danger">删除</a>
    //                         <a href="" class="btn btn-warning">修改</a>

    //                     </td>
    //                 </tr>`
    //     })
    // }
    
    $.ajax({
        url:"/index",
        type:"get",
        data:{},
        success:function(res){
            let data = res.data;
            // console.log(data);
            let str = "";
            data.forEach((item)=>{
                str += `
                <tr class="data">
                        <td>${item.id}</td>
                        <td>${item.name}</td>
                        <td>${item.sex}</td>
                        <td>${item.age}</td>
                        <td>
                            <a class="btn btn-danger" cid="${item.id}">删除</a>
                            <a class="btn btn-warning" cid="${item.id}">修改</a>
                        </td>
                    </tr>
                `
            })
            let tb = $("tbody");
            tb.html(str);
        }
    })
    $("#tb").on("click",".btn-danger", function () {
        console.log(123);
        //alert("触发")
        // 获取id值 发送后端
        let id = $(this).attr("cid");
        console.log(id);
        $.ajax({
            url:"/del",
            type:"get",
            data:{id:id},
            success:function(res){
                if(res.code == 400){
                    alert(res.mag)
                }else{
                    alert("删除成功")
                    window.location = res.mag
                }
            }
        })
    })

    $("#tb").on("click", ".btn-warning", function () {
        let id = $(this).attr("cid");
        console.log(id);
        $.ajax({
            url:"/index",
            type:"get",
            data:{
                id:id
            },
            success:function(res){
                if(res.code == 200){
                    window.location = res.mag1+"?id="+id
                }
            }
        })
    })
</script>